package com.iraeta.api;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.iraeta.HikConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service(value = "hikApi")
public class HikApi {
    private static final Logger logger = LoggerFactory.getLogger(HikApi.class);

    @Autowired
    private HikConfig hikConfig;

    public final String  ARTEMIS_PATH = "/artemis";

    public List<String> getAllMenJin() throws Exception{
        String url = ARTEMIS_PATH + "/api/resource/v2/door/search";
        Map<String, String> path = new HashMap<String, String>(3) {
            {
                put("https://", url); //Key 为协议，Value 为接口路径
            }
        };
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo", 1);
        jsonBody.put("pageSize", 100);
        jsonBody.put("name", "4F");
        String body = jsonBody.toJSONString();
        String result = ArtemisHttpUtil.doPostStringArtemis(new ArtemisConfig(hikConfig.getHost(), hikConfig.getAppkey(), hikConfig.getAppsecret()),
                path, body, null, null,"application/json;charset=UTF-8");
        if(StrUtil.isEmpty(result)){
            throw new RuntimeException("调用HIK接口[door/search]未知错误");
        }
        JSONObject jsonResult = JSONObject.parseObject(result);
        String code = jsonResult.getString("code");
        if(!code.equals("0")){
            throw new RuntimeException("调用HIK接口[door/search]错误:" + jsonResult.getString("msg"));
        }

        JSONObject jsonResultData = jsonResult.getJSONObject("data");
        JSONArray doorList = jsonResultData.getJSONArray("list");
        List<String> doorIndexCodes = new ArrayList<>();
        for(int i=0; i<doorList.size();i++){
            JSONObject door = doorList.getJSONObject(i);
            String indexCode = door.getString("indexCode");
            doorIndexCodes.add(indexCode);
        }
        return doorIndexCodes;
    }

    public int controlDoor(int controlType) throws Exception{
        String url = ARTEMIS_PATH + "/api/acs/v1/door/doControl";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", url); //Key 为协议，Value 为接口路径
            }
        };
        String[] doorIndexCodes = new String[]{"5b7e1664d8a64303b9edcab50df29d8d", "45943eebbde144239a9a9c2fc27519c0"};
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("doorIndexCodes", doorIndexCodes);
        jsonBody.put("controlType", controlType);
        String body = jsonBody.toJSONString();
        String result = ArtemisHttpUtil.doPostStringArtemis(new ArtemisConfig(hikConfig.getHost(), hikConfig.getAppkey(), hikConfig.getAppsecret()),
                path, body, null, null,"application/json;charset=UTF-8");
        if(StrUtil.isEmpty(result)){
            throw new RuntimeException("调用HIK接口[door/doControl]未知错误");
        }
        JSONObject jsonResult = JSONObject.parseObject(result);
        String code = jsonResult.getString("code");
        if(!code.equals("0")){
            throw new RuntimeException("调用HIK接口[door/doControl]错误:" + jsonResult.getString("msg"));
        }
        return 1;
    }

    public int controlDoor4floor(int controlType) throws Exception{
        String url = ARTEMIS_PATH + "/api/acs/v1/door/doControl";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", url); //Key 为协议，Value 为接口路径
            }
        };
        String[] doorIndexCodes = new String[]{"5c69eed4624e404cab33b0f55238abfe", "5676a1baa23848f89df1386666af4162"};
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("doorIndexCodes", doorIndexCodes);
        jsonBody.put("controlType", controlType);
        String body = jsonBody.toJSONString();
        String result = ArtemisHttpUtil.doPostStringArtemis(new ArtemisConfig(hikConfig.getHost(), hikConfig.getAppkey(), hikConfig.getAppsecret()),
                path, body, null, null,"application/json;charset=UTF-8");
        if(StrUtil.isEmpty(result)){
            throw new RuntimeException("调用HIK接口[door/doControl]未知错误");
        }
        JSONObject jsonResult = JSONObject.parseObject(result);
        String code = jsonResult.getString("code");
        if(!code.equals("0")){
            throw new RuntimeException("调用HIK接口[door/doControl]错误:" + jsonResult.getString("msg"));
        }
        return 1;
    }
}
